// Connect.h : Declaration of the CConnect

#pragma once
#include "resource.h"       // main symbols
#include "PageRememberSettings.h"

_ATL_FUNC_INFO DocumentOpenInfo = {CC_STDCALL,VT_EMPTY,1, {VT_DISPATCH|VT_BYREF}};
_ATL_FUNC_INFO DocumentBeforeCloseInfo = {CC_STDCALL,VT_EMPTY,2, {VT_DISPATCH|VT_BYREF, VT_BOOL|VT_BYREF}};
// CConnect
class ATL_NO_VTABLE CConnect : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CConnect, &CLSID_Connect>,
	public IDispatchImpl<AddInDesignerObjects::_IDTExtensibility2, &AddInDesignerObjects::IID__IDTExtensibility2, &AddInDesignerObjects::LIBID_AddInDesignerObjects, 1, 0>,
	public IDispEventSimpleImpl<1,CConnect,     
         &__uuidof(MSWord::ApplicationEvents2)>
{
public:
	CConnect()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_ADDIN)
DECLARE_NOT_AGGREGATABLE(CConnect)

BEGIN_COM_MAP(CConnect)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(AddInDesignerObjects::IDTExtensibility2)
END_COM_MAP()

BEGIN_SINK_MAP(CConnect)
SINK_ENTRY_INFO(1,__uuidof(MSWord::ApplicationEvents2),4, DocumentOpen, &DocumentOpenInfo)
SINK_ENTRY_INFO(1, __uuidof(MSWord::ApplicationEvents2), 6, DocumentBeforeClose, &DocumentBeforeCloseInfo)
END_SINK_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

	CString GetFullPath(CComQIPtr<_Document>& spDoc);

	CPageRememberSettings m_rememberSettings;

public:
	//IDTExtensibility2 implementation:
	STDMETHOD(OnConnection)(IDispatch * Application, AddInDesignerObjects::ext_ConnectMode ConnectMode, IDispatch *AddInInst, SAFEARRAY **custom);
	STDMETHOD(OnDisconnection)(AddInDesignerObjects::ext_DisconnectMode RemoveMode, SAFEARRAY **custom );
	STDMETHOD(OnAddInsUpdate)(SAFEARRAY **custom );
	STDMETHOD(OnStartupComplete)(SAFEARRAY **custom );
	STDMETHOD(OnBeginShutdown)(SAFEARRAY **custom );

	void __stdcall DocumentOpen(IDispatchPtr ptr);
	void __stdcall DocumentBeforeClose(IDispatchPtr ptr, VARIANT_BOOL* cancel);

	CComPtr<IDispatch> m_pApplication;
	CComPtr<IDispatch> m_pAddInInstance;
};

OBJECT_ENTRY_AUTO(__uuidof(Connect), CConnect)
